"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const maishu_node_mvc_1 = require("maishu-node-mvc");
const website_config_1 = require("./website-config");
const path = require("path");
const fs = require("fs");
const ts = require("typescript");
let componetsDir = website_config_1.componetsDirectory;
exports.currentAppId = maishu_node_mvc_1.createParameterDecorator(async (req) => {
    let appId = req.headers["application-id"];
    return appId;
});
function getClientComponentInfos(sourcePath) {
    let componentInfos = [];
    let staticPath = path.join(sourcePath, "static");
    let componentsPath = path.join(staticPath, componetsDir);
    console.assert(fs.existsSync(componentsPath));
    let dirs = [componentsPath];
    let files = [];
    while (dirs.length > 0) {
        let dir = dirs.pop();
        let items = fs.readdirSync(dir);
        for (let i = 0; i < items.length; i++) {
            let itemPath = path.join(dir, items[i]);
            let stat = fs.statSync(itemPath);
            if (stat.isDirectory()) {
                dirs.push(itemPath);
            }
            else if (stat.isFile()) {
                files.push(itemPath);
            }
        }
    }
    files.forEach(fileName => {
        let tsSourceFile = ts.createSourceFile(fileName, fs.readFileSync(fileName).toString(), ts.ScriptTarget.ES5);
        let componentPath = path.relative(staticPath, fileName);
        componentPath = componentPath.replace(/\\/g, "/");
        componentPath = componentPath.replace(".tsx", ".js");
        let c = getFileComponentInfos(tsSourceFile, componentPath);
        componentInfos.push(...c);
    });
    function getFileComponentInfos(sourceFile, filePath) {
        let componentInfos = [];
        delintNode(sourceFile, filePath);
        return componentInfos;
    }
    function delintNode(node, filePath) {
        switch (node.kind) {
            case ts.SyntaxKind.ClassDeclaration:
                const DISPLAY_NAME = "displayName";
                const NAME = "name";
                const ICON = "icon";
                const INTRODUCE = "introduce";
                let propNames = [NAME, DISPLAY_NAME, ICON, INTRODUCE];
                let classDeclaration = node;
                if (classDeclaration.decorators) {
                    var decorator = classDeclaration.decorators.filter(o => o.expression != null && o.expression.expression != null
                        && o.expression.expression.text == "component")[0];
                    if (decorator == null)
                        break;
                    let componentInfo = {};
                    componentInfo.name = classDeclaration.name.text;
                    componentInfo.path = filePath;
                    var expr = decorator.expression;
                    let arg0 = expr.arguments[0];
                    if (arg0 != null) {
                        let properties = arg0.properties.filter(o => o.kind == ts.SyntaxKind.PropertyAssignment);
                        for (let i = 0; i < propNames.length; i++) {
                            let propName = propNames[i];
                            let prop = properties.filter(o => o.name.kind == ts.SyntaxKind.Identifier && o.name.text == propName)[0];
                            if (prop) {
                                componentInfo[propName] = prop.initializer.text;
                            }
                        }
                    }
                    componentInfos.push(componentInfo);
                }
                break;
        }
        ts.forEachChild(node, (node) => delintNode(node, filePath));
    }
    return componentInfos;
}
exports.getClientComponentInfos = getClientComponentInfos;
exports.settings = {};
